package com.yxq.mdt.app.modules.video.ui.fragment;


import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.orhanobut.hawk.Hawk;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseFragment;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.modules.personal.bean.MyPersonalBean;
import com.yxq.mdt.app.modules.video.adapter.NearbyAdapter;
import com.yxq.mdt.app.modules.video.bean.AdvertBean;
import com.yxq.mdt.app.modules.video.bean.ChannelListBean;
import com.yxq.mdt.app.modules.video.bean.MLUserList;
import com.yxq.mdt.app.modules.video.bean.NearbyBannerBean;
import com.yxq.mdt.app.modules.video.bean.VideoNearbyBean;
import com.yxq.mdt.app.modules.video.bean.VideoNearbyListBean;
import com.yxq.mdt.app.modules.video.model.MainVideoModel;
import com.yxq.mdt.app.modules.video.presenter.IMainVideoContract;
import com.yxq.mdt.app.modules.video.presenter.MainVideoPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 密聊-附近
 */
public class VideoNearbyFragment extends BaseFragment<MainVideoPresenter> implements IMainVideoContract.View {
    public static final String EXTRA_TEXT = "extra_text";
    @BindView(R.id.nearby_recycler)
    RecyclerView nearbyRecycler;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    NearbyAdapter nearbyAdapter;
    @BindView(R.id.error_view)
    RelativeLayout errorView;

    @Override
    protected int attachLayoutId() {
        return R.layout.fragment_video_nearby_fragement;
    }

    @Override
    protected MainVideoPresenter getPresenter() {
        return new MainVideoPresenter(this, new MainVideoModel());
    }


    @Override
    public void initView(View view) {
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.advertByPosition("A3");
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getMlList(page, 20, 2, null, Hawk.get(SPKey.SP_KEY_USER_ADDRESS_MAP_LNG) + "", Hawk.get(SPKey.SP_KEY_USER_ADDRESS_MAP_LAT) + "");
        });
        nearbyRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        MyPersonalBean personalBean = Hawk.get(SPKey.SP_KEY_USER_MY_INFO);
        String localUserId = "";
        if (personalBean != null) {
            localUserId = personalBean.getUserId() + "";
        }
        nearbyAdapter = new NearbyAdapter(null, localUserId);
        nearbyRecycler.setAdapter(nearbyAdapter);
        nearbyAdapter.setEmptyView(R.layout.layout_empty_view, nearbyRecycler);
        nearbyAdapter.isUseEmpty(false);
    }

    @Override
    public void initData() {
        refreshLayout.autoRefresh();
    }

    /**
     * 滑动页面至顶部并刷新数据
     */
    public void refresh() {
        if (nearbyAdapter != null && nearbyAdapter.getData() != null && nearbyAdapter.getData().size() > 0) {
            nearbyRecycler.scrollToPosition(0);
        }
        refreshLayout.autoRefresh();
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void onChannelListSuccess(List<ChannelListBean> channelListBeanList) {

    }

    @Override
    public void onChannelListFailed(String error) {

    }

    @Override
    public void onUserListSuccess(MLUserList userList) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();

        VideoNearbyBean videoNearbyBean = new VideoNearbyBean();
        List<VideoNearbyListBean> list = new ArrayList<>();

        for (MLUserList.ListBean u : userList.getList()) {
            String price = u.getSoundsVideoStatus() == 1 ? (u.getGsLiveGrade().getVoicePrice() + "豆/分钟") : (u.getGsLiveGrade().getVideoPrice() + "豆/分钟");

            int voiceLen = 0;
            String voicePath = null;
            if (u.getVoiceFile() != null) {
                voiceLen = u.getVoiceFile().getFileLength();
                voicePath = u.getVoiceFile().getFilePath();
            }

            VideoNearbyListBean videoChatListBean = new VideoNearbyListBean(price, voiceLen, u.getHeadimg(), u.getResidence(), u.getHeadimg(), u.getGeneration(), u.getAge() + "", u.getGsName(), u.getSoundsVideoStatus(), u.getVideoDetails(), String.valueOf(u.getSex()), u.getHeadimg(), voicePath, u.getDistance());
            videoChatListBean.setUserId(u.getUserId());
            videoChatListBean.setPrice(u.getSoundsVideoStatus() == 1 ? (u.getGsLiveGrade().getVoicePrice()) : (u.getGsLiveGrade().getVideoPrice()));
            list.add(videoChatListBean);
        }
        videoNearbyBean.setRecommendBeans(list);
        if (userList.getList() != null && userList.getList().size() != 0) {
            nearbyAdapter.addData(videoNearbyBean);
        } else {
            refreshLayout.finishLoadMoreWithNoMoreData();
        }
    }

    @Override
    public void onUserListFailed(String error) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        //异常情况 加载默认没数据的View
        VideoNearbyBean videoNearbyBean = new VideoNearbyBean();
        List<VideoNearbyListBean> list = new ArrayList<>();
        videoNearbyBean.setRecommendBeans(list);
        nearbyAdapter.addData(videoNearbyBean);
    }

    @Override
    public void onAdvertSuccess(AdvertBean advertBean) {
        errorView.setVisibility(View.GONE);
        if (page == 1 && nearbyAdapter.getData() != null) {
            nearbyAdapter.getData().clear();
        }
        //顶部广告列表
        NearbyBannerBean nearbyBannerBean = new NearbyBannerBean();
        VideoNearbyListBean videoNearbyListBean;
        List<VideoNearbyListBean> videoNearbyListBeans = new ArrayList<>();
        for (AdvertBean.ListBean l : advertBean.getList()) {
            videoNearbyListBean = new VideoNearbyListBean(null, 0, l.getImageUrl(), null, l.getImageUrl(), null, null, null, 0, null, null, l.getImageUrl(), null, null);
            videoNearbyListBeans.add(videoNearbyListBean);
        }

        nearbyBannerBean.setBannerListBean(videoNearbyListBeans);

        nearbyAdapter.addData(nearbyBannerBean);

        nearbyAdapter.isUseEmpty(!(nearbyAdapter.getData().size() > 0));

        mPresenter.getMlList(page, 20, 2, null, Hawk.get(SPKey.SP_KEY_USER_ADDRESS_MAP_LNG) + "", Hawk.get(SPKey.SP_KEY_USER_ADDRESS_MAP_LAT) + "");
    }

    @Override
    public void onAdvertSuccessFailed(String error) {
        errorView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (nearbyAdapter.countDownTimer != null) {
            nearbyAdapter.countDownTimer.cancel();
        }
    }
}
